package com.hn.gege.sys.modules.tt.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hn.gege.core.common.BaseController;
import com.hn.gege.core.common.CommonPage;
import com.hn.gege.core.common.CommonResult;
import com.hn.gege.core.query.QueryGenerator;
import com.hn.gege.sys.modules.tt.entity.Tc;
import com.hn.gege.sys.modules.tt.service.ITcService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;


@Api(tags = "tt-TcController", description = "管理")
@RestController
@RequestMapping("/tt/tc")
@Validated
public class TcController extends BaseController {

    @Autowired
    ITcService tcService;

    @ApiOperation("分页列表")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "pageNo", value = "当前页", required = true, dataType = "Integer", paramType = "query"),
        @ApiImplicitParam(name = "pageSize", value = "每页数量", required = true, dataType = "Integer", paramType = "query"),
    })
    @GetMapping("/pageInfo")
    public CommonPage<Tc> pageInfo(@NotNull Integer pageNo,
                                   @NotNull Integer pageSize,
                                   Tc tc,
                                   HttpServletRequest request){
        Page<Tc> pageInfo = new Page<>(pageNo, pageSize);
        QueryWrapper<Tc> qw = QueryGenerator.initQueryWrapper(tc, request.getParameterMap());
        Page<Tc> page1 = tcService.page(pageInfo, qw);
        return CommonPage.restPage(page1);
    }

    @ApiOperation("通过主键获取")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "主建", required = true, dataType = "String", paramType = "path")
    })
    @GetMapping("/{id}")
    public CommonResult<Tc> getByPk(@NotBlank@PathVariable("id") String id){
        Tc tc = tcService.getByPk(id);
        return CommonResult.success(tc);
    }

    @ApiOperation("新增")
    @PostMapping
    public CommonResult add(@Validated Tc tc, BindingResult bindingResult){
        boolean b = tcService.save(tc);
        if (b){
            return CommonResult.success("成功");
        }
        return CommonResult.failed("失败");
    }

    @ApiOperation("修改")
    @PutMapping
    public CommonResult updateByPk(@Validated Tc tc, BindingResult bindingResult){
        boolean b = tcService.updateByPk(tc);
        if (b){
            return CommonResult.success("成功");
        }
        return CommonResult.failed("失败");
    }


    @ApiOperation("通过主键删除")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "主建", required = true, dataType = "String", paramType = "path")
    })
    @DeleteMapping("/{id}")
    public CommonResult removeByPk(@NotBlank@PathVariable("id") String id){
        boolean b = tcService.removeByPk(id);
        if (b){
            return CommonResult.success("成功");
        }
        return CommonResult.failed("失败");
    }


    @ApiOperation("修改测试")
    @PutMapping("/updateTest")
    public CommonResult updateTest(){
        Tc tc = new Tc();
        tc.setId(1);
        tc.setTname("张三");
        tcService.updateTest(tc);
        if (true){
            return CommonResult.success("成功");
        }
        return CommonResult.failed("失败");
    }

    @ApiOperation("删除测试")
    @PutMapping("/deleteTest")
    public CommonResult deleteTest(){
        Tc tc = new Tc();
        tc.setId(1);
        tcService.deleteTest(tc);
        if (true){
            return CommonResult.success("成功");
        }
        return CommonResult.failed("失败");
    }



}
